package Queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author lyx
 * @Date 2021/6/25 16:14
 */
public class BlockingQueueTest {
    public static void main(String[] args) throws InterruptedException {
        test4();
    }

    /**
    *抛出异常
    */
    public static void test1(){
        //FIFO
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println(arrayBlockingQueue.add("a"));
        System.out.println(arrayBlockingQueue.add("b"));
        System.out.println(arrayBlockingQueue.add("c"));

        //获取首元素
        System.out.println(arrayBlockingQueue.element());
        //java.lang.IllegalStateException: Queue full
        System.out.println(arrayBlockingQueue.add("d"));


        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());

        //java.util.NoSuchElementException
        System.out.println(arrayBlockingQueue.remove());
    }

    /**
     * 不抛出异常
     * 有返回值 false null
     * */
    public static void test2(){
        //FIFO
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println(arrayBlockingQueue.offer("a"));
        System.out.println(arrayBlockingQueue.offer("b"));
        System.out.println(arrayBlockingQueue.offer("c"));

        //获取首元素
        System.out.println(arrayBlockingQueue.peek());
        //java.lang.IllegalStateException: Queue full
        System.out.println(arrayBlockingQueue.offer("d"));


        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());

        //java.util.NoSuchElementException
        System.out.println(arrayBlockingQueue.poll());
    }

    /**
     * 阻塞等待
     * 一直等待等到死
     * */
    public static void test3() throws InterruptedException {
        //FIFO
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(3);

        arrayBlockingQueue.put("a");
        arrayBlockingQueue.put("b");
        arrayBlockingQueue.put("c");

        //一直等待
        arrayBlockingQueue.put("d");

        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());

        //一直等待
        System.out.println(arrayBlockingQueue.take());
    }

    /**
     * 超时等待
     * 在一段事件内等待,若无果,退出等待
     * */
    public static void test4() throws InterruptedException {
        //FIFO
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println(arrayBlockingQueue.offer("a"));
        System.out.println(arrayBlockingQueue.offer("b"));
        System.out.println(arrayBlockingQueue.offer("c"));

        //等待2s,无果退出
        //加入内容,参数,参数单位
        System.out.println(arrayBlockingQueue.offer("d",2, TimeUnit.SECONDS));

        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());

        //等待2s,无果退出
        System.out.println(arrayBlockingQueue.poll(3,TimeUnit.SECONDS));
    }


}
